//
//  main.m
//  NSArray详解
//
//  Created by 大欢 on 16/5/24.
//  Copyright © 2016年 大欢. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"


int main(int argc, const char * argv[]) {
    @autoreleasepool {

        //一个数组可以存不同类型的对象 字面量写法 @[]
        NSArray * arr1 = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
        
        //根据索引获取元素
        NSString * index = [arr1 objectAtIndex:2];
        NSLog(@"%@",index);
        //字面量写法
        NSLog(@"%@",arr1[2]);
        
        //是否包含containsObject 会执行isEqual方法
        
        NSString * idx1 = @"hjk";
        NSString * idx2 = @"hwk";
        //判断两个对象是否相等
        if ([idx1 isEqualToString:idx2]) {
            NSLog(@"isEqual yes");
        } else {
            NSLog(@"isEqual no");
        }
        
        if ([arr1 containsObject:@"4"]) {
            NSLog(@"yes");
        } else {
            NSLog(@"no");
        }
        
        Person * p1 = [[Person alloc] init];
        p1.name = @"dahuan";
        p1.age = 20;
        
        Person * p2 = [[Person alloc] init];
        p2.name = @"ade";
        p2.age = 2;
        
        if ([p1 isEqual:p2]) {
            NSLog(@"p1 isEqual:p2");
        } else {
            NSLog(@"not p1 isEqual:p2");
        }
        
        Person * p3 = [[Person alloc] init];
        p3.name = @"xiaohong";
        p3.age = 18;
        
        NSArray * personList = @[p1,p2];
        
        if ([personList containsObject:p3]) {
            NSLog(@"person yes");
        } else {
            NSLog(@"person no");
        }
        
        
        
//        NSMutableArray * personList2 = [NSMutableArray arrayWithArray:@[p1,p2]];
//
//        //快速枚举
//        //in 左边枚举的每一项  in 右边是个集合
//        for (Person * p in personList2) {
//            
//            if (p.age ==  20) {
//                //不要在快速枚举里修改数组，不安全
//                [personList2 removeObject:p];
//            }
//        }
//        NSLog(@"%@",personList2);
        
        //快速枚举，基本不用 //reverseObjectEnumerator 反向遍历
        NSEnumerator * enperson = [personList objectEnumerator];
        id aaa;
        while (aaa = [enperson nextObject]) {
            Person * p = (Person *)aaa;
            
            NSLog(@"%@",p.name);
        }
        
        //快速枚举//常用
        [personList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
//            NSLog(@"%d",*stop);
            
            if (idx == 1) {
            
                NSLog(@"obj = %@ idx = %ld stop = %p",obj,idx,stop);
                *stop = YES ;
                
            }
        }];
        
        //数组里所有的元素发送一条消息，告诉他跑
        [personList makeObjectsPerformSelector:@selector(run)];
        
        //根据对象返回索引
        
        NSInteger idxx = [personList indexOfObject:p1];
        NSLog(@"%ld",idxx);
        
        //给已有元素添加新数组
        NSArray * newArray = [personList arrayByAddingObject:p3];
        NSLog(@"%@",newArray);
        
        //截取子数组
        NSRange range = {1,1};
        NSArray * subArray = [newArray subarrayWithRange:range];
        NSLog(@"%@",subArray);
        
        
        //排序数组中元素
        NSArray * sortArray = [newArray sortedArrayUsingSelector:@selector(compareAge:)];
        NSLog(@"%@",sortArray);
//        
//        NSArray * sortArray2 = [newArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
//            
//            Person * p1 = (Person *)obj1;
//            Person * p2 = (Person *)obj2;
//            
//            if (p1.age > p2.age) {
//                return NSOrderedAscending;
//            } else if (p1.age < p2.age) {
//                return NSOrderedDescending;
//            }
//            return NSOrderedSame;
//            
//        }];
//        NSLog(@"%@",sortArray2);
//        
        
        
    }
    return 0;
}
